package com.yantong.controller.api;

import com.yantong.annotation.Auth;
import com.yantong.controller.BaseController;
import com.yantong.exception.BDException;
import com.yantong.mapper.MemberMapper;
import com.yantong.mapper.TBusinessCardMapper;
import com.yantong.pojo.Member;
import com.yantong.pojo.TBusinessCard;
import com.yantong.result.ResultVo;
import com.yantong.service.*;
import com.yantong.utils.ValidateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * @author: juzi
 * @date: 2021/10/25
 * @time: 16:12
 */
@ApiIgnore
@Controller
@RequestMapping("/wxapi/member")
public class ApiMemberController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(ApiMemberController.class);


    @Autowired
    private TBusinessCardMapper tBusinessCardMapper;
    @Autowired
    private CommissionService commissionService;
    @Autowired
    private MemberService memberService;
    @Autowired
    private MemberRoleService memberRoleService;
    @Autowired
    private CentreService centreService;
    @Autowired
    private WithdrawalsService withdrawalsService;
    @Autowired
    private MemberIntegralService integralService;
    @Autowired
    private MemberMapper memberMapper;



    /**
     * 获取会员信息和门店信息
     */
    @RequestMapping("")
    @ResponseBody
    @Auth(arg = 0)
    public ResultVo getMember(Long id, Boolean getShop) {
        Member member = memberService.getDetail(id);
        ResultVo resultVo = new ResultVo();
        if (member == null) {
            resultVo.put("success", true);
            resultVo.put("data", "");
            resultVo.put("points", "");
            resultVo.put("time", System.currentTimeMillis());
            return resultVo;
        }

        //用户预计佣金收益
        Double waitGrantCommission = commissionService.commissionTotal(member.getId());
        member.setWaitGrantCommission(waitGrantCommission == null ? 0.00 : waitGrantCommission);
        resultVo.put("success", true);
        resultVo.put("data", member);

        List<String> roleList = memberRoleService.queryList(id);
        resultVo.put("roleList", roleList);

        if (member.getUpshopTid() != null) {
            Member shop = memberService.getDetail(member.getUpshopTid());
            resultVo.put("shop", shop);
        }

        //用户所属的店铺信息
        if (member.getTeamid() != null) {
            Member shop = memberService.getDetail(member.getTeamid());
            resultVo.put("userShop", shop);
        }

        if (member.getCentreId() != null) {
            resultVo.put("centre", centreService.getCentreById(member.getCentreId()));
        }

        /**
         * 查询会员积分
         */
        double points = integralService.getMemberIntegralNew(id);
        resultVo.put("points", points);
        resultVo.put("time", System.currentTimeMillis());
        //已提现金额
        resultVo.put("cashCount", withdrawalsService.getMemberCashedCount(id));
        return resultVo;
    }


    /**
     * 更新会员信息
     */
    @RequestMapping("/update")
    @ResponseBody
    @Auth
    public ResultVo updateMember(@RequestBody Member member) {
        if (ValidateUtil.isNotEmptyString(member.getMobile())) {
            //查询手机号码是否以被绑定
            Member member1 = memberMapper.getByMobileCenter(member.getMobile(), member.getCentreId());
            if (member1 != null && member.getId().intValue() != member1.getId().intValue()) {
                throw new BDException("该手机号码已有其他用户绑定，请更换手机号码重试！");
            }
        }
        if (member.getUpshopTid() != null) {
            logger.info("---------------链接绑定了门店--------------");
            //得到用户信息
            Member m = memberService.getTmByKey(member.getId());
            if (m.getTeamid() != null) {
                logger.info("---------------已经绑定了门店--------------");
                ResultVo resultVo = new ResultVo();
                resultVo.put("success", false);
                return resultVo;
            } else {
                logger.info("---------------绑定了门店--------------");
                //门店信息
                Member mem = memberService.getTmByKey(member.getUpshopTid());
                member.setTeamid(member.getUpshopTid());
                member.setCentreId(mem.getCentreId() == null ? 0 : mem.getCentreId());
            }
        }
        int result = memberService.updateMember(member);

        if (result < 1) {
            return ResultVo.error();
        }

        ResultVo resultVo = new ResultVo();
        resultVo.put("success", true);
        return resultVo;
    }
}
